<?php

namespace app\api\controller\carbon\calculation;

use app\common\controller\Api;
use think\Db;

/**
 * 核算接口
 */
class Info extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 获取计算项
     *
     */
    public function index($type_id = '', $level = 2)
    {

        if ($level == 1) {

            $where = array(
                'type_superior_id' => $type_id
            );
        } else {
            $where = array(
                'type_id' => $type_id
            );
        }
        $list = model('app\admin\model\carbon\calculation\Info')
            ->where($where)
            ->field("id,name,image,unit_name,unit_symbol")
            ->select();
        $calculation = [];
        foreach ($list as $key => $value) {
            $energyList = model('app\admin\model\carbon\energy\Info')
                ->with('type')
                ->where('calculation_id', $value->id)
                ->select();
            $energy = [];
            foreach ($energyList as $key => $val) {
                $data = array(
                    "id" => $val->id,
                    "name" => $val->type->name,
                    "image" => $val->type->image,
                );
                $energy[] = $data;
            }
            $calculationData = array(
                "id" => $value->id,
                "name" => $value->name,
                "image" => $value->image,
                "unit_name" => $value->unit_name,
                "unit_symbol" => $value->unit_symbol,
                "energy" => $energy,
            );
            $calculation[] = $calculationData;
        }
        $this->success('获取成功', $calculation);
    }

    /**
     * 计算
     *
     */
    public function count()
    {
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            $user_id =  $this->auth->id;
            // $user_id = 8;
            $value = $this->request->post('value');
            $calculation_id = $this->request->post('calculation_id');
            $energy_id = $this->request->post('energy_id');

            $calculation = model('app\admin\model\carbon\calculation\Info')->where('id', $calculation_id)->find();

            $energy = model('app\admin\model\carbon\energy\Info')->where('id', $energy_id)->find();
            $coefficient_province = model('app\admin\model\carbon\coefficient\Info')->where('superior_id', $energy['coefficient_id'])->find();
            if ($coefficient_province) {
                $coefficientValue = $coefficient_province['value'];
            } else {
                $coefficient = model('app\admin\model\carbon\coefficient\Info')->where('id', $energy['coefficient_id'])->find();
                $coefficientValue = $coefficient['value'];
            }
            $user = model('app\common\model\User')->where('id', $user_id)->find();

            $schoolData = model('app\admin\model\carbon\campus\School')->where('id', $user->school_id)->find();
            $result = $this->accounting($value, $coefficientValue);
            $result = $this->delZero($result);
            $calculation_log = [
                "user_id" => $user_id,
                "input_value" => $value,
                "result" => $result,
                "type_superior_id" => $calculation->type_superior_id,
                "type_id" => $calculation->type_id,
                "calculation_id" => $calculation->id,
                "province_id" => $schoolData->province_id,
                "city_id" => $schoolData->city_id,
                "school_id" => $schoolData->id,
                "create_time" => time(),
            ];
            $re = model('app\admin\model\carbon\calculation\Log')->insert($calculation_log);
            if ($re) {
                $this->success('获取成功', $result);
            }
            $this->error('网络错误，请重试！');
        }
        $this->error(__('非法请求'));
    }



























































    /**
     * 获取系数 (内部方法)
     */
    private function accounting($value, $coefficientValue)
    {
        $result = bcmul($value, $coefficientValue, 5);
        return  $result;
    }
    /*
     * 去掉数字小数点后的0
     */
    private function delZero($s)
    {
        $s = trim(strval($s));
        if (preg_match('#^-?\d+?\.0+$#', $s)) {
            return preg_replace('#^(-?\d+?)\.0+$#', '$1', $s);
        }
        if (preg_match('#^-?\d+?\.[0-9]+?0+$#', $s)) {
            return preg_replace('#^(-?\d+\.[0-9]+?)0+$#', '$1', $s);
        }
        return $s;
    }
}
